package cn.vcjf.weapp.api.user.service.impl;

import cn.vcjf.weapp.api.user.entity.User;
import cn.vcjf.weapp.api.user.entity.UserCash;
import cn.vcjf.weapp.api.user.entity.UserWallet;
import cn.vcjf.weapp.api.user.mapper.UserCashMapper;
import cn.vcjf.weapp.api.user.mapper.UserMapper;
import cn.vcjf.weapp.api.user.mapper.UserWalletMapper;
import cn.vcjf.weapp.api.user.service.UserWalletService;
import cn.vcjf.weapp.common.util.IdGen;
import cn.vcjf.weapp.common.util.weixin.WxWithdrawReq;
import cn.vcjf.weapp.common.util.weixin.WxWithdrawRes;
import cn.vcjf.weapp.common.util.weixin.WxWithdrawServer;
import cn.vcjf.weapp.common.util.weixin.WxWithdrawUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import netscape.javascript.JSObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Service("userWalletService")
public class UserWalletServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet> implements UserWalletService {

	@Resource
	private UserMapper userMapper;
	@Resource
	private UserCashMapper userCashMapper;

	@Override
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public boolean doUserWithdraw(UserWallet userWallet) {


		userWallet.preUpdate();
		baseMapper.doUserWithdraw(userWallet);

	/*insert into vc_user_cash (
          userId, cashMoney, cashOrderNo, cashStatus, cashDes, currency,
          settlePoundage, actualMoney, accountName, bankPhoneNo, bankCardNo,
          bankName, pMsg, pAllRes, createUser, createTime, updateUser, updateTime, deleteFlag, sysUserId
      */

		User user = userMapper.selectById(userWallet.getUserId());

		UserCash userCach = new UserCash();
		userCach.setUserId(userWallet.getUserId());
		userCach.setCashMoney(userWallet.getCashMoney());

		userCach.setCashOrderNo(IdGen.getAutoOrderNo());
		userCach.setCashStatus(1);//(1,提现中,2成功,3失败)
		userCach.setCashDes("提现中");
		userCach.setCurrency("CNY");

		userCach.setSettlePoundage(userWallet.getCashMoney());
		userCach.setActualMoney(userWallet.getCashMoney());

		userCach.setAccountName(StringUtils.isNotBlank(user.getName())?user.getName():user.getNickname());
		userCach.setBankPhoneNo(user.getPhone());

		userCach.preInsert();
		userCashMapper.insert(userCach);

		Map<String, String> params = new HashMap<>();
		params.put("orderNo",userCach.getCashOrderNo());
		params.put("bankCardNo",userCach.getBankCardNo());
		params.put("bankCode",userCach.getBankCardNo());
		params.put("amount",userCach.getActualMoney().toString());
		params.put("realName",userCach.getAccountName());
		params.put("openid",user.getOpenId());
		params.put("desc","分润提现");

		boolean b = WxWithdrawUtil.cashWalletToWeChat(new WxWithdrawServer() {
			@Override
			public WxWithdrawReq buildWxWithdrawReq(Map<String, String> params) {
				return WxWithdrawUtil.buildWxWithdrawReq(params);
			}

			@Override
			public void saveOrder(Map<String, String> params) {

			}

			@Override
			public void doFailed(WxWithdrawRes res) {
				int row = 0;
				try {
					row = baseMapper.doRollBackUserWithdraw(userWallet);
				}catch (Exception e){
					e.printStackTrace();
				}
				UserCash userCach = new UserCash();
				userCach.setUserId(userWallet.getUserId());
				userCach.setSysUserId(userWallet.getSysUserId());
				userCach.setCashStatus(3);//(1,提现中,2成功,3失败)
				String msg = "提现失败,回滚成功";
				if(row == 0){
					msg = "提现失败,回滚失败:" + JSONObject.toJSONString(userWallet);
				}
				userCach.setCashDes(msg);
				userCach.preInsert();
				userCashMapper.updateUserCashByUserId(userCach);

			}

			@Override
			public void doSuccess(WxWithdrawRes res) {
				UserCash userCach = new UserCash();
				userCach.setUserId(userWallet.getUserId());
				userCach.setSysUserId(userWallet.getSysUserId());
				userCach.setCashStatus(2);//(1,提现中,2成功,3失败)
				userCach.setCashDes("提现成功");

				userCach.preInsert();
				userCashMapper.updateUserCashByUserId(userCach);
			}

			@Override
			public void doException() {
				int row = 0;
				try {
					row = baseMapper.doRollBackUserWithdraw(userWallet);
				}catch (Exception e){
					e.printStackTrace();
				}
				UserCash userCach = new UserCash();
				userCach.setUserId(userWallet.getUserId());
				userCach.setSysUserId(userWallet.getSysUserId());
				userCach.setCashStatus(3);//(1,提现中,2成功,3失败)
				String msg = "提现异常,回滚成功";
				if(row == 0){
					msg = "提现异常,回滚失败:" + JSONObject.toJSONString(userWallet);
				}
				userCach.setCashDes(msg);
				userCach.preInsert();
				userCashMapper.updateUserCashByUserId(userCach);

			}
		}, params);

		return b;
	}
}
